package Graphics
{
	import EventDispatchers.ImageLoader;
	
	import Events.ImageEvent;
	
	import flash.display.Bitmap;
	
	public class Animation
	{
		protected var loop:Boolean = false;
		protected var frameWidth:int;
		protected var frameTime:Number = 0;		
		protected var currentFrame:int = 0;
		private var graphic:TileSet;
		private var name:String;
		private var parent:GameObject
		private var frameNumber:int;
		
		public function Animation(parent:GameObject, name:String, image:String, frameNumber:int, loop:Boolean = true, fps:int = 30)
		{
			this.loop = loop;
			this.parent = parent;
			this.frameNumber = frameNumber;
			LoadTileMap(image);
			this.name = name;
			this.frameTime = fps;
		}
		
		private function LoadTileMap(xml:String):void
		{
			var loader:ImageLoader = new ImageLoader();
			loader.addEventListener(ImageEvent.IMAGELOADED, complete);
			loader.Load(xml);
		}
		
		private function complete(e:ImageEvent):void
		{
			var bitmap:Bitmap = e.Data;
			graphic = new TileSet(bitmap, parent.Width,parent.Height);
			this.frameWidth = graphic.TileWidth;
		}
		public function get Loop():Boolean{return loop;}
		public function get FrameWidth():int {return frameWidth;}
		public function get FrameNumber():int {return frameNumber;}
		public function get FrameTime():Number {return frameTime;}
		public function get Graphic():TileSet {return graphic;}
		public function get Name():String {return name;}
		
		public function Destroy():void
		{
			graphic = null;
		}
	}
}